<template>
  <div class="module-management">
    <div class="module-header">
      <el-button type="primary" icon="el-icon-plus" @click="showAddModuleDialog">添加模块</el-button>
      <el-input
        v-model="searchQuery"
        placeholder="搜索模块"
        prefix-icon="el-icon-search"
        clearable
        class="search-input"
      ></el-input>
    </div>

    <el-table 
      :data="filteredModules" 
      style="width: 100%" 
      v-loading="loading"
      border
      stripe
      highlight-current-row>
      <el-table-column prop="name" label="模块名称" min-width="150">
        <template slot-scope="scope">
          <div class="module-name">
            <i class="el-icon-folder"></i>
            <span>{{ scope.row.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" min-width="200" show-overflow-tooltip />
      <el-table-column label="操作" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="primary" 
            icon="el-icon-edit"
            @click="editModule(scope.row)">
            编辑
          </el-button>
          <el-button 
            size="mini" 
            type="danger" 
            icon="el-icon-delete"
            @click="deleteModule(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑模块对话框 -->
    <el-dialog 
      :title="dialogTitle" 
      :visible.sync="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @closed="resetForm">
      <el-form 
        :model="moduleForm" 
        :rules="rules" 
        ref="moduleForm" 
        label-width="80px"
        class="module-form">
        <el-form-item label="模块名称" prop="name">
          <el-input v-model="moduleForm.name" placeholder="请输入模块名称"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input 
            type="textarea" 
            v-model="moduleForm.description" 
            :rows="3"
            placeholder="请输入模块描述">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModule" :loading="submitting">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { moduleService } from '@/services/projectService'

export default {
  name: 'ModuleManagement',
  data() {
    return {
      loading: false,
      submitting: false,
      modules: [],
      searchQuery: '',
      dialogVisible: false,
      dialogTitle: '添加模块',
      moduleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入模块名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入模块描述', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    filteredModules() {
      if (!this.searchQuery) return this.modules
      const query = this.searchQuery.toLowerCase()
      return this.modules.filter(module => 
        module.name.toLowerCase().includes(query) ||
        module.description.toLowerCase().includes(query)
      )
    }
  },
  methods: {
    showAddModuleDialog() {
      this.dialogTitle = '添加模块'
      this.dialogVisible = true
    },
    editModule(row) {
      this.dialogTitle = '编辑模块'
      this.moduleForm = { ...row }
      this.dialogVisible = true
    },
    deleteModule(row) {
      this.$confirm('确认删除该模块吗？', '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        moduleService.deleteModule(row.id)
        this.loadModules()
        this.$message.success('删除成功')
        this.$root.$emit('modules-updated')
      }).catch(() => {})
    },
    submitModule() {
      this.$refs.moduleForm.validate((valid) => {
        if (valid) {
          this.submitting = true
          try {
            moduleService.saveModule(this.moduleForm)
            this.dialogVisible = false
            this.loadModules()
            this.$message.success('保存成功')
            this.$root.$emit('modules-updated')
          } catch (error) {
            this.$message.error('保存失败：' + error.message)
          } finally {
            this.submitting = false
          }
        }
      })
    },
    resetForm() {
      if (this.$refs.moduleForm) {
        this.$refs.moduleForm.resetFields()
      }
      this.moduleForm = {
        name: '',
        description: ''
      }
    },
    loadModules() {
      this.loading = true
      try {
        this.modules = moduleService.getModules()
      } catch (error) {
        this.$message.error('加载模块列表失败：' + error.message)
      } finally {
        this.loading = false
      }
    }
  },
  created() {
    this.loadModules()
  }
}
</script>

<style scoped>
.module-management {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.module-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-input {
  width: 300px;
}

.module-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.module-name i {
  color: #409EFF;
}

.module-form {
  padding: 20px;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-dialog__body) {
  padding: 0;
}
</style> 